package com.study.www.controller;

import com.study.www.enums.ConfigEnum;
import com.study.www.exception.ResponseEnum;
import com.study.www.model.JobConfig;
import com.study.www.model.Result;
import com.study.www.service.ConfigService;
import com.study.www.service.JobConfigService;
import org.quartz.CronScheduleBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: :
 * @date :Create in  2023/8/14- 11:14
 * @Version: V1.0
 * @Modified By:
 **/
@Controller
public class JobController {

    private final Logger LOGGER=LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ConfigService configService;

    @Autowired
    JobConfigService tableService;

    @GetMapping("/test")
    @ResponseBody
    public String getEp(){
        ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(null);
        return "ok";
    }
    @GetMapping("/test2")
    @ResponseBody
    public String getEp2(@Validated JobConfig jobConfig){
        System.out.println(jobConfig);
        configService.testVil();
        return "ok";
    }
    @GetMapping("/test3")
    @ResponseBody
    public String test3(@Validated JobConfig jobConfig){
        System.out.println(jobConfig);
        return "ok";
    }

    @GetMapping("/index")
    public String table(ModelMap map){
        List<JobConfig> configs = configService.findAll();
        for (JobConfig config : configs) {
            String message = ConfigEnum.findByMessage(config.getStatus());
            config.setStatus(message);
        }
        map.put("configs",configs);
        return "/index";
    }

    @PostMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Long id, String status){
        Result result = new Result();
        result.setResult(false);
        try {
            tableService.update(id,status);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }

    @PostMapping("/nowRun")
    @ResponseBody
    public Result nowRun(Long id){
        Result result = new Result();
        result.setResult(false);
        try {
            tableService.nowRunJob(id);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }

    @PostMapping("/updateQuartz")
    @ResponseBody
    public Result updateQuartz(Long id, String cron){
        Result result = new Result();
        result.setResult(false);
        try {
            CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(cron);
        }catch (Exception e){
            result.setMsg("cron表达式有误！！");
            return result;
        }
        try {
            tableService.updateCron(id,cron);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }



}
